package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.mapper.havexml.AdvanSearchMapper;
import com.sinog.biz.mapper.havexml.DepartmentMapper;
import com.sinog.biz.mapper.havexml.SystemCodeMapper;
import com.sinog.biz.service.business.AdvanSearchService;
import com.sinog.core.domain.AdvanSearch;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 数据统计分析 - 多维数据检索 - 多维数据检索 - AdvanSearchServiceImpl
 * @author -
 * @date -
 */
@Service
public class AdvanSearchServiceImpl extends ServiceImpl<AdvanSearchMapper,AdvanSearch> implements AdvanSearchService {

    @Resource
    private AdvanSearchMapper advanSearchMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private SystemCodeMapper systemCodeMapper;

    /**
     * 数据统计分析 - 多维数据检索 - 多维数据检索 - 保存检索条件
     * @param map map
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveSqltext(Map<String,Object> map) {
        String sqltext = map.get("sqltext").toString();
        if(StringUtils.isNotBlank(sqltext)) {
            //清空上次数据
            advanSearchMapper.deleteAdvansearch(map);
            //插入数据
            AdvanSearch advanSearch = new AdvanSearch();
            advanSearch.setSqltext(sqltext);
            advanSearch.setCreatetime(new Date());
            advanSearch.setCreateid(map.get("createid").toString());
            advanSearch.setType(map.get("type").toString());
            boolean tag = advanSearch.insertOrUpdate();
            if(tag) {
                return Result.success("保存成功");
            }
        } else {
            //清空上次数据
            advanSearchMapper.deleteAdvansearch(map);
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 数据统计分析 - 多维数据检索 - 多维数据检索 - 查询检索条件
     * @param map map
     * @return String
     */
    @Override
    public String selectSqltext(Map<String,Object> map) {
        return advanSearchMapper.selectSqltext(map);
    }

    /**
     * 数据统计分析 - 多维数据检索 - 多维数据检索 - 获取省份信息
     * @param loginUserDepartId loginUserDepartId
     * @return Result
     */
    @Override
    public Result getProvince(String loginUserDepartId) {
        List<Map<String,Object>> list = systemCodeMapper.getProvince(loginUserDepartId);
        Map<String,Object> map = new HashMap<>(16);
        map.put("proList",list);
        map.put("nativePro",loginUserDepartId);
        return Result.success(map);
    }

    /**
     * 数据统计分析 - 多维数据检索 - 多维数据检索 - 获取监狱单位数据
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getJailList() {
        return departmentMapper.getJailList();
    }
}